package com.youlai.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.system.common.result.Result;
import com.youlai.system.mapper.StockMedicinesMapper;
import com.youlai.system.mapper.StockProviderMapper;
import com.youlai.system.model.entity.StockMedicines;
import com.youlai.system.model.entity.StockProvider;
import com.youlai.system.model.entity.Stock_emdicines;
import com.youlai.system.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @作业 chen
 * @时间 2024-09-02 16:24
 */
@RestController
@RequestMapping("/api/v1/StockMedicines")
@RequiredArgsConstructor
@Slf4j
public class StockmedicinesController {
    private final StockMedicinesMapper stockMedicinesMapper;

    @RequestMapping("/list")
    public Result list(@RequestBody Stock_emdicines stock_emdicines) {
        System.out.println("1111111111111111111---------------------");
        Page page = new Page(stock_emdicines.getCurrentPage4(), stock_emdicines.getPageSize4());
        LambdaQueryWrapper<StockMedicines> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!stock_emdicines.getName().isEmpty(), StockMedicines::getMedicinesName, stock_emdicines.getName());
        wrapper.like(!stock_emdicines.getKeywords().isEmpty(), StockMedicines::getKeywords, stock_emdicines.getKeywords());
        wrapper.eq(!stock_emdicines.getManufactor().isEmpty(), StockMedicines::getMedicinesId, stock_emdicines.getManufactor());
        wrapper.eq(!stock_emdicines.getDrugType().isEmpty(), StockMedicines::getMedicinesType, stock_emdicines.getDrugType());
        wrapper.eq(!stock_emdicines.getStatus().isEmpty(), StockMedicines::getStatus, stock_emdicines.getStatus());
        Page page1 = stockMedicinesMapper.selectPage(page, wrapper);
        return Result.success(page1);
    }

    @PostMapping("/add")
    @Transactional
    public Result add(@RequestBody StockMedicines stockMedicines) {
        LambdaQueryWrapper<StockMedicines> wrapper = new LambdaQueryWrapper<>();
        System.out.println(stockMedicines);
        wrapper.eq(StockMedicines::getMedicinesName, stockMedicines.getMedicinesName());
        wrapper.eq(StockMedicines::getMedicinesNumber, stockMedicines.getMedicinesNumber());
        wrapper.eq(StockMedicines::getKeywords, stockMedicines.getKeywords());
        wrapper.eq(StockMedicines::getMedicinesType, stockMedicines.getMedicinesType());
        wrapper.eq(StockMedicines::getPrescriptionType, stockMedicines.getPrescriptionType());
        wrapper.eq(StockMedicines::getUnit, stockMedicines.getUnit());
        wrapper.eq(StockMedicines::getConversion, stockMedicines.getConversion());
        wrapper.eq(StockMedicines::getPrescriptionPrice, stockMedicines.getPrescriptionPrice());
        wrapper.eq(StockMedicines::getMedicinesStockDangerNum, stockMedicines.getMedicinesStockDangerNum());
        wrapper.eq(StockMedicines::getStatus, stockMedicines.getStatus());
        wrapper.eq(StockMedicines::getCreateBy, SecurityUtils.getUsername());


        StockMedicines one = stockMedicinesMapper.selectOne(wrapper);
        if (one == null) {
            stockMedicinesMapper.insert(stockMedicines);
            return Result.success();
        } else {
            return Result.failed("该药品已存在！");
        }
    }

    //根据id查询数据
    @RequestMapping("/getMedicinesById/{producterId}")
    public Result getMedicinesById(@PathVariable("producterId") String producterId) {
        LambdaQueryWrapper<StockMedicines> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StockMedicines::getMedicinesId, producterId);
        StockMedicines stockMedicines = stockMedicinesMapper.selectOne(wrapper);
        return Result.success(stockMedicines);
    }

    //修改方法
    @PostMapping("/update")
    @Transactional
    public Result update(@RequestBody StockMedicines stockMedicines) {
        System.out.println(stockMedicines);
        LambdaUpdateWrapper<StockMedicines> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StockMedicines::getMedicinesId, stockMedicines.getMedicinesId())
                .set(StockMedicines::getMedicinesName, stockMedicines.getMedicinesName())
                .set(StockMedicines::getMedicinesNumber, stockMedicines.getMedicinesNumber())
                .set(StockMedicines::getKeywords, stockMedicines.getKeywords())
                .set(StockMedicines::getMedicinesType, stockMedicines.getMedicinesType())
                .set(StockMedicines::getPrescriptionType, stockMedicines.getPrescriptionType())
                .set(StockMedicines::getUnit, stockMedicines.getUnit())
                .set(StockMedicines::getConversion, stockMedicines.getConversion())
                .set(StockMedicines::getPrescriptionPrice, stockMedicines.getPrescriptionPrice())
                .set(StockMedicines::getMedicinesStockDangerNum, stockMedicines.getMedicinesStockDangerNum())
                .set(StockMedicines::getStatus, stockMedicines.getStatus())
        ;
        stockMedicinesMapper.update(wrapper);
        return Result.success();
    }

    //删除方法
    @PostMapping("/delete/{id}")
    @Transactional
    public Result delete(@PathVariable("id") String id) {
        System.out.println(id);
        LambdaQueryWrapper<StockMedicines> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StockMedicines::getMedicinesId, id);
        stockMedicinesMapper.delete(wrapper);
        return Result.success();
    }



}
